package com.demo.gulimall.order.controller;

import com.demo.gulimall.common.utils.R;
import com.demo.gulimall.order.entity.Order;
import com.demo.gulimall.order.service.OrderService;
import com.demo.gulimall.order.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Chen Yuan
 * @create 2023-06-26  16:35
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
	@Autowired
	private OrderService orderService;

	// 查询订单确认页面所需数据
	@GetMapping("/toTrade")
	public R toTrade(@RequestParam("userId") Long userId){
		OrderConfirmVo orderConfirm = null;
		try {
			orderConfirm = orderService.confirmOrder(userId);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return R.ok(orderConfirm);
	}


	// 提交订单
	@PostMapping("/submit")
	public R submitOrder(@RequestBody OrderSubmitVo orderSubmitVo){
		OrderSubmitResVo submitRes = null;
		try {
			submitRes = orderService.submitOrder(orderSubmitVo);
		}catch (Exception e){
			log.error("--> submitOrder - 提交订单 - msg:{} \n",  e.getMessage(), e);
			return R.error(("--> submitOrder - 提交订单 - msg:" + e.getMessage()));
		}

		log.info("--> submitOrder - 提交订单完成 - 结果:{} \n",  submitRes.isSuccess());
		if (submitRes.isSuccess()) {
			return R.ok(submitRes);
		}else {
			return R.error(submitRes.getMsg());
		}
	}


	@GetMapping("/status")
	public R<Order> getOrderStatus(@RequestParam("orderSn") String orderSn){
		Order order = orderService.getOrderBySn(orderSn);
		return R.ok(order);
	}
}
